package by.qizer.supercamp.web;

import by.qizer.supercamp.domain.Auto;
import by.qizer.supercamp.service.AutoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * @author Pribytkoyski Alexey
 *         User: qizer
 *         Date: 08.04.12
 */
@Controller
public class AutoController {

    @Autowired
    private AutoService autoService;

    @RequestMapping("/auto")
    public String listAuto(Map<String, Object> map) {
        map.put("auto", new Auto());
        map.put("autoList", autoService.listAuto());
        return "auto";
    }

    @RequestMapping(value = "/autoadd", method = RequestMethod.POST)
    public String addAuto(@ModelAttribute("auto") Auto auto, BindingResult result) {
        autoService.addAuto(auto);
        return "redirect:/auto";
    }

    @RequestMapping("/autodel/{autoId}")
    public String deleteAuto(@PathVariable("autoId") Integer autoId) {
        autoService.removeAuto(autoId);
        return "redirect:/auto";
    }
}
